<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 权限管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-form>
          <el-form-item label="角色">
            <el-select
                v-model="roleId"
                @change="handleSearch"
                class="handle-select mr10"
            >
              <el-option
                  v-for="r in roleList.list"
                  :key="r.sid"
                  :label="r.name"
                  :value="r.sid"
              ></el-option>
              <!-- <el-option key="2" label="辅导员" value="2"></el-option>
              <el-option key="3" label="财务管理员" value="3"></el-option>
              <el-option key="4" label="后勤部" value="4"></el-option> -->
            </el-select>
          </el-form-item>
        </el-form>

        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          icon="el-icon-circle-plus-outline"-->
        <!--          @click="handleAdd"-->
        <!--          >新增角色</el-button-->
        <!--        >-->
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          icon="el-icon-circle-plus-outline"-->
        <!--          @click="handleDelete"-->
        <!--          >删除角色</el-button-->
        <!--        >-->
      </div>
      <div class="handle-check" v-for="(p, index) in rolePerm.perms" :key="p">
        <el-checkbox
          v-model="p.checkAll"
          :indeterminate="p.isIndeterminate"
          @change="handleCheckAllChange($event, index)"
          >{{ p.title }}</el-checkbox
        >
        <el-checkbox-group
          v-model="p.checkedList"
          @change="handleCheckedChange($event, index)"
        >
          <el-checkbox v-for="sub in p.subs" :key="sub" :label="sub.sid">{{
            sub.title
          }}</el-checkbox>
        </el-checkbox-group>
      </div>
      <el-button type="primary" @click="saveEdit">保存</el-button>
    </div>
    <el-dialog
      title="添加角色"
      v-model="addVisible"
      width="30%"
      @close="getData"
    >
      <el-form :model="form.data" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input
            v-model="form.data.name"
            class="handle-input mr10"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addVisible = false">取消</el-button>
          <el-button type="primary" @click="addRole">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from "vue";
import request from "../../utils/request";
import { ElMessage, ElMessageBox } from "element-plus";

export default {
  name: "Permission",
  setup() {
    const roleList = reactive({
      list: [],
    });
    // 查询操作
    const getData = () => {
      return request.get("/role/noAdmin").then((res) => {
        roleList.list = res.data;
      });
    };

    const rolePerm = reactive({
      perms: [
        {
          checkAll: false,
          isIndeterminate: false,
          checkedList: [],
          sid: "",
          title: "",
          subs: [
            {
              title: "",
              sid: "",
            },
          ],
        },
      ],
    });

    const roleId = ref("2");

    // 查询操作
    const handleSearch = (val) => {
      request.get("/role/perm/" + val).then((res) => {
        rolePerm.perms = res.data;
      });
    };

    const saveEdit = () => {
      request
        .post(
          "/role/perm/" + roleId.value,
          "permission=" + JSON.stringify(rolePerm.perms)
        )
        .then((res) => {
          if (res.code === "0") {
            ElMessage.success("保存成功！");
          } else {
            ElMessage.error("保存失败！");
          }
        });
    };

    const handleCheckAllChange = (val, index) => {
      rolePerm.perms[index].checkedList = val
        ? rolePerm.perms[index].subs.map((sub) => sub.sid)
        : [];
      rolePerm.perms[index].isIndeterminate = false;
    };
    const handleCheckedChange = (value, index) => {
      const checkedCount = value.length;
      rolePerm.perms[index].checkAll =
        checkedCount === rolePerm.perms[index].subs.length;
      rolePerm.perms[index].isIndeterminate =
        checkedCount > 0 && checkedCount < rolePerm.perms[index].subs.length;
    };

    // 删除操作
    const handleDelete = () => {
      // 二次确认删除
      ElMessageBox.confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          request.delete("/role/" + roleId.value).then((res) => {
            if (res.data) {
              ElMessage.success("删除成功");
              getData().then(() => {
                roleId.value =
                  roleList.list.length > 0 ? roleList.list[0].sid : "";
                handleSearch(roleId.value);
              });
            } else {
              ElMessage.error("删除失败");
            }
          });
        })
        .catch(() => {});
    };

    const addVisible = ref(false);

    var form = reactive({
      data: {
        name: "",
      },
    });

    // 新增操作
    const handleAdd = () => {
      addVisible.value = true;
    };
    const addRole = () => {
      request({
        url: "/role",
        method: "POST",
        data: form.data,
      }).then((res) => {
        ElMessage.success("保存成功！");
        form.data.name = "";
        addVisible.value = false;
      });
    };

    onMounted(() => {
      getData().then(() => {
        roleId.value = roleList.list.length > 0 ? roleList.list[0].sid : "";
        handleSearch(roleId.value);
      });
    });

    return {
      roleList,
      rolePerm,
      roleId,
      form,
      addVisible,
      getData,
      addRole,
      handleSearch,
      saveEdit,
      handleCheckAllChange,
      handleCheckedChange,
      handleDelete,
      handleAdd,
    };
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-check {
  margin-bottom: 20px;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
